package EBM.UI;

import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import EBM.BLL.SocketConnection;
import EBM.BLL.Util;

public class ModifyUser extends org.eclipse.swt.widgets.Composite {

	
	public static Display display;
	public static Shell shell;

	public static String userID;
	public static String userName;
	public static String userType;
	private Text txtUserName;
	private Text txtUserId;
	private Button btnReview;
	private Button btnCommonUser;
	private Button btnAdmin;
	private Button button2;
	private Button btnCancel;
	private Label lblUserType;
	private Label lblUserName;
	private Label lblUserId;
	
	private MessageBox box1 = new MessageBox(shell, SWT.YES | SWT.ICON_INFORMATION);
	private MessageBox box2 = new MessageBox(shell, SWT.YES | SWT.ICON_ERROR);
	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	/*public static void main(String[] args) {
		showGUI();
	}*/
		
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {
		display = Display.getDefault();
		shell = new Shell(display, SWT.CLOSE | SWT.MIN);
		ModifyUser inst = new ModifyUser(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		shell.setText("修改用户信息");
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.setLocation(display.getClientArea().width / 2 - shell.getSize().x/2, 
				display.getClientArea().height / 2 - shell.getSize().y/2);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public ModifyUser(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
		InitData();
	}

	private void initGUI() {
		try {
			this.setLayout(null);
		
			this.setSize(380, 240);
			{
				lblUserId = new Label(this, SWT.NONE);
				lblUserId.setText("用户ID");
				lblUserId.setBounds(35, 40, 60, 24);
			}
			{
				lblUserName = new Label(this, SWT.NONE);
				lblUserName.setText("用户姓名");
				lblUserName.setBounds(35, 80, 60, 22);
			}
			{
				lblUserType = new Label(this, SWT.NONE);
				lblUserType.setText("用户类型");
				lblUserType.setBounds(35, 120, 60, 26);
			}
			{
				btnCancel = new Button(this, SWT.PUSH | SWT.CENTER);
				btnCancel.setText("确定");
				btnCancel.setBounds(100, 175, 58, 24);
				btnCancel.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						btnCancelWidgetSelected(evt);
						shell.close();
					}
				});
			}
			{
				button2 = new Button(this, SWT.PUSH | SWT.CENTER);
				button2.setText("取消");
				button2.setBounds(205, 175, 59, 24);
				button2.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						shell.close();
					}
				});
			}
			{
				btnAdmin = new Button(this, SWT.RADIO | SWT.LEFT);
				btnAdmin.setText("系统用户");
				btnAdmin.setBounds(105, 120, 66, 26);
			}
			{
				btnCommonUser = new Button(this, SWT.RADIO | SWT.LEFT);
				btnCommonUser.setText("普通用户");
				btnCommonUser.setBounds(190, 120, 69, 26);
			}
			{
				btnReview = new Button(this, SWT.RADIO | SWT.LEFT);
				btnReview.setText("审查用户");
				btnReview.setBounds(275, 120, 69, 26);
			}
			{
				txtUserId = new Text(this, SWT.BORDER);
				txtUserId.setBounds(115, 40, 152, 24);
				txtUserId.setEditable(false);
			}
			{
				txtUserName = new Text(this, SWT.BORDER);
				txtUserName.setBounds(115, 80, 152, 21);
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void InitData(){
		txtUserId.setText(userID);
		txtUserName.setText(userName);
		if(userType.equals("系统用户")){
			btnAdmin.setSelection(true);
			btnCommonUser.setSelection(false);
			btnReview.setSelection(false);
		}else if(userType.equals("普通用户")){
			btnAdmin.setSelection(false);
			btnCommonUser.setSelection(true);
			btnReview.setSelection(false);
		}else if(userType.equals("审查用户")){
			btnAdmin.setSelection(false);
			btnCommonUser.setSelection(false);
			btnReview.setSelection(true);
		}
	}
	
	private void btnCancelWidgetSelected(SelectionEvent evt) {
		try {
			SocketConnection.setCmd("fixuser");
			ArrayList<Object> out = new ArrayList<Object>();
				
			out.add(userID);
			
			if(txtUserName.getText().equals("")){
				box1.setMessage("用户名不能为空:");
				box1.open();
				return;
			}
			
			if (Util.isValidUserName(txtUserName.getText().trim()) == false) {
				box1.setMessage("用户名只能由字母、中文、数字、下划线构成，且只能以字母或中文开头，长度不能超过12位!");
				box1.open();
				return;
			}
			out.add(txtUserName.getText().trim());
			
			/*if(btnAdmin.getSelection()&&!btnCommonUser.getSelection()&&!btnReview.getSelection())
				out.add("0");
			if(!btnAdmin.getSelection()&&btnCommonUser.getSelection()&&!btnReview.getSelection())
				out.add("1");
			if(!btnAdmin.getSelection()&&!btnCommonUser.getSelection()&&btnReview.getSelection())
				out.add("2");
				*/
			if(btnAdmin.getSelection())
				out.add("0");
			else if(btnCommonUser.getSelection())
				out.add("1");
			else
				out.add("2");
			
			SocketConnection.setListOut(out);
			SocketConnection.DataTransmission();
			if (SocketConnection.getListIn() == null || SocketConnection.getListIn().size() == 0)
			{
				box1.setMessage("服务器连接出错，请检查配置文件和网络连接！");
				box1.open();
				//return;
				System.exit(0);
			}
			
			ArrayList<Object> in = SocketConnection.getListIn();
			if(in.get(0).toString().equals("1")){
				box1.setMessage("修改成功！");
			    box1.open();
			    return;
			}else{
				box2.setMessage("修改失败！");
			    box2.open();
			    return;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
